﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;
using FinanSoft.Utilities;

namespace FinanSoft
{
    public partial class frmMoneda : Form
    {

        private int[] _currencyIds;

        public frmMoneda()
        {
            InitializeComponent();
            fillGridMoneda();
        }

        public void fillGridMoneda()
        {
            gridMoneda.Rows.Clear();
            List<Currency> currencies = MoneyManager.GetInstance().GetAddedCurrencies();
            currencies.Add(MoneyManager.GetInstance().SystemCurrency);
            if (!MoneyManager.GetInstance().IsError)
            {
                _currencyIds = new int[currencies.Count];
                for (int currencyIndex = 0; currencyIndex < _currencyIds.Length; currencyIndex++)
                {
                    Currency currency = currencies[currencyIndex];
                    gridMoneda.Rows.Add(new object[] 
                    { currency.Name, currency.Abbreviation, currency.Symbol,
                        (currency.ToLocalCurrency == 0 ? MessageConstants.NO_EXCHANGE_RATE_ESTABLISHED : currency.ToLocalCurrency.ToString()) });
                    _currencyIds[currencyIndex] = currency.Id;
                }
            }
            else
                MessageBox.Show(MoneyManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnDefinirMoneda_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario MonedasImportantes
            frmMonedasImportantes frmMonedasImportantesV = new frmMonedasImportantes();
            frmMonedasImportantesV.ShowDialog(this);     // Mostramos el formulario de forma modal.
        }

        private void btnCrearMoneda_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Moneda
            frmCrearMoneda frmCrearMonedaV = new frmCrearMoneda(Constants.INVALID_ID);
            frmCrearMonedaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
            fillGridMoneda();
        }

        private void btnEditarMoneda_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Moneda
            if (gridMoneda.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_CURRENCY, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int editedId = _currencyIds[gridMoneda.SelectedRows[0].Index];
                frmCrearMoneda frmCrearMonedaV = new frmCrearMoneda(editedId);
                frmCrearMonedaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
                fillGridMoneda();
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnEliminarMoneda_Click(object sender, EventArgs e)
        {
            if (gridMoneda.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_CURRENCY, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int deletedId = _currencyIds[gridMoneda.SelectedRows[0].Index];
                MoneyManager.GetInstance().DeleteCurrency(deletedId);
                if (MoneyManager.GetInstance().IsError)
                    MessageBox.Show(MoneyManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    MessageBox.Show(MessageConstants.CURRENCY_DELETED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    fillGridMoneda();
                }
            }
        }
    }
}
